﻿using System.Web;
using System.Web.Services;
using Ext.Net;
using Demo.CommonBase.View.Forms;
using Demo.Services.View.Controllers;
using Demo.Services.Contracts.DTO.Filters;
using System.Collections.Generic;
using Demo.Services.Contracts.DTO.Infos;
using System;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.CommonBase.DTO.Filters;
using Demo.CommonBase.DTO.Infos;

namespace Demo.Web.Common
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Resource :PageBase, IHttpHandler 
    {

        public void ProcessRequest(HttpContext context)
        {
            CommonController cont;
            CommonComboBoxPagingInfo resources;
            ComboBoxFilter filter;            
            try
            {
                Logger.Info();
                context.Response.ContentType = "text/json";
                cont = new CommonController();

                filter = new ComboBoxFilter();
                filter.Pagination = new PaginationInfo();
                filter.Pagination.PageNum = !string.IsNullOrEmpty(context.Request["start"]) ? int.Parse(context.Request["start"]) : 0;
                filter.Pagination.PageSize = !string.IsNullOrEmpty(context.Request["limit"]) ? int.Parse(context.Request["limit"]) : 10;
                filter.Name = !string.IsNullOrEmpty(context.Request["query"]) ? context.Request["query"] : string.Empty;

                SetValue("ComboBoxFilter", filter);
                cont.ExecuteService(this, "Services.GetResourceByName");
                resources = (CommonComboBoxPagingInfo)GetValue("Resources");

                if (resources != null)
                {
                    context.Response.Write(string.Format("{{total:{1},'resources':{0}}}", JSON.Serialize(resources.DataList), resources.Pagination.TotalCount));
                }


            }
            catch(Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }            
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
